<?php
/**
 * @desc 商品管理
 * @author kevin wang
 * @date 2014-10-25
 */
class ctrl_formula extends ctrl_base {
    private static $dirName = 'formula';
    
    /**
     * @desc 商品列表
	 * @author kevin wang
	 * @param void
	 * @return void
     */
    public static function main() {
    	if(!model_admin::islogin()) {
    		self::goto_login();
    	}
		self::operation(array('formula_list'));
		//页面输入参数处理
    	$currentPage = isset($_POST['pageNum']) ? $_POST['pageNum'] : 1;
    	$pagesize = isset($_POST['numPerPage']) ? $_POST['numPerPage'] : self::$_pagesize;
		$nextpage = ($currentPage - 1) * $pagesize;

    	$formula_name = isset($_POST['formula_name']) ? trim($_POST['formula_name']) : '';
		$express = isset($_POST['express']) ? trim($_POST['express']) : '';
    	//搜索条件组合
		$condition = array();
    	if(!empty($formula_name)) {
    		$condition['formula_name'] = $formula_name;
    	}
		if(!empty($express)) {
    		$condition['express'] = $express;
    	}
		//列表获取
    	$formula_list = model_formula :: get_formula_list($nextpage,$pagesize,$condition);
    	$total = array_pop($formula_list);
		//模板赋值
    	self::smarty() -> assign('formula_list', $formula_list);
		self::smarty() -> assign('total', $total);
		self::smarty() -> assign('pageNum', $currentPage);
		self::smarty() -> assign('numPerPage', $pagesize);
		self::smarty() -> assign('pageNumShown', self::$_pageNumShown);
		self::smarty() -> assign('formula_name', $formula_name);
		self::smarty() -> assign('express', $express);
		self::smarty() -> assign('shoNum', self::$_shoNum);
		
    	self::smarty() -> display(self::$dirName . '/formula_list.html');    	
    }
	//添加和修改转发
	public static function formula_do(){
		if(!model_admin::islogin()) {
    		self::goto_login();
    	}
		$produce_id = isset($_GET['produce_id']) ? intval($_GET['produce_id']) : 0;
		$produce = model_formulalist::get_produce_info($produce_id);
		if($produce['formula_id'] == 0){
			header("location:/index.php?act=formula&st=formula_add&produce_id={$produce['product_id']}");exit;
		}else{
			header("location:/index.php?act=formula&st=formula_edit&id={$produce['formula_id']}&produce_id={$produce['product_id']}");exit;
		}
	}
	//添加商品
	public static function formula_add(){
		if(!model_admin::islogin()) {
    		self::goto_login();
    	}
		self::operation(array('formula_add'));
		//商品ID
		$produce_id = isset($_GET['produce_id']) ? intval($_GET['produce_id']) : 0;
		if($_POST){

			$formula = self::get_formula_data();

			//$express = model_formula::get_express($formula['express']);

			//$formula['express'] = $express['express'];

			$formula['editer'] = $_SESSION['admin']['username'];
			$formula['author'] = $_SESSION['admin']['username'];

			$result = model_formula::formula_add($formula);
			if(!empty($result)){
				self::ajax_ouput(200, '操作成功','formula_main','closeCurrent');
			}else{
				self::ajax_ouput(300, '添加失败');
			}
		}
		//查询商品信息
		$prduce = model_produce::get_produce_by_id($produce_id);
		//用户分类
		$category = model_news::get_user_category();
		self::smarty() -> assign('category', $category);
		//价格字段
		$price_list = model_price :: get_price_list(array(),array(),array('price_id'=>$prduce['parent_id']));
		$total = array_pop($price_list);
		if(empty($price_list)){
			self::ajax_ouput(300, '请先配置大类价格模版','','closeCurrent');
		}
		$price_list = self::build_tree($price_list, 0);
		$price_list = self::count_array_childs($price_list);
		self::smarty() -> assign("price_list", $price_list);
		self::smarty() -> assign("prduce", $prduce);
		//产品大分类
		/*$big_class = model_produce::get_type_list();
		self::smarty() -> assign("big_class", $big_class);
		//产品小分类
		$small_class = model_formula::get_small_class($big_class[0]['product_class_id']);
		self::smarty() -> assign("small_class", $small_class);*/
		self::smarty() -> display(self::$dirName . '/formula_add.html');
	}
	//删除商品
	/*public static function formula_delete() {
    	if(!model_admin::islogin()) {
			self::goto_login();
    	}
		self::operation(array('formula_delete'));
    	$ids = $_POST['ids'];
		$ids = explode(",", $ids);
		foreach($ids as $v){
			$v = intval($v);
			$id[] = $v;
		}
    	$data = array('delete_flag' => 1);

    	$st = model_formula::update_formula_info($id, $data);

    	if($st > 0) {
			self::ajax_ouput(200, '操作成功','formula_main');
    	} else {
			self::ajax_ouput(300, '操作失败');
    	}
    }*/
	//编辑商品
	 public static function formula_edit() {
    	if(!model_admin::islogin()) {
    		self::goto_login();
    	}
		$id = isset($_GET['id'])? (intval($_GET['id'])):(intval($_POST['id']));
    	if($id <= 0) {
			self::ajax_ouput(300, '非法访问');
    	}
		$formula = model_formula::get_formula_by_id($id);
		$ctrl_tag = 'formula_edit_other';
		if($formula['author'] == $_SESSION['admin']['username']){
			$ctrl_tag = 'formula_edit_self';
		}
		self::operation(array($ctrl_tag));
		//商品ID
		$produce_id = isset($_GET['produce_id']) ? intval($_GET['produce_id']) : 0;
    	if($_POST){
			$produce_id=$formula['small_class_id'];
			$formula = array();

			$formula = self::get_formula_data();

			//$express = model_formula::get_express($formula['express']);

			//$formula['express'] = $express['express'];

			$formula['editer'] = $_SESSION['admin']['username'];
			$formula['produce_id'] = $produce_id;
			unset($formula['id']);

			$result = model_formula::update_formula_info(explode(',',$id), $formula);
	    	
		    if(!empty($result)){
				self::ajax_ouput(200, '操作成功','formula_main','closeCurrent');
			}else{
				self::ajax_ouput(300, '操作失败');
			}    		
    	}
		//查询商品信息
		$prduce = model_produce::get_produce_by_id($produce_id);
		//用户分类
		$category = model_news::get_user_category();
		self::smarty() -> assign('category', $category);
		//价格字段
		$price_list = model_price :: get_price_list(array(),array(),array('price_id'=>$prduce['parent_id']));
		$total = array_pop($price_list);
		if(empty($price_list)){
			self::ajax_ouput(300, '请先配置大类价格模版','','closeCurrent');
		}
		$price_list = self::build_tree($price_list, 0);
		$price_list = self::count_array_childs($price_list);
		self::smarty() -> assign("price_list", $price_list);
		//产品大分类
		/*$big_class = model_produce::get_type_list();
		self::smarty() -> assign("big_class", $big_class);
		//产品小分类
		$small_class = model_formula::get_small_class($formula['big_class_id']);
		self::smarty() -> assign("small_class", $small_class);*/

		$formula['visiblearr'] = self::analyze_user_category($formula['visible']);
    	self::smarty() -> assign('formula', $formula);
    	self::smarty() -> display(self::$dirName . '/formula_edit.html');
    }
	//公式表格生成
	public static function formula_table(){
		if(!model_admin::islogin()) {
    		self::goto_login();
    	}
		self::operation(array('formula_table'));
		$produce_id = isset($_GET['produce_id']) ? intval($_GET['produce_id']) : 0;
		$produce = model_formulalist::get_produce_info($produce_id);
		$id = intval($produce['formula_id']);
		if(empty($id)){
			self::ajax_ouput(300, '操作失败,尚未设定价格计算公式','formula_table','closeCurrent');
		}
		//类信息
		$formula_table = model_formula::get_class_info($id);
		/*if(!empty($formula_table['basic_price'])){
			foreach($formula_table['basic_price'] as $k => $v){
				$formula_table['basic_price'][$k]['marerial'] = json_decode($formula_table['basic_price'][$k]['marerial'], true);
			}
		}*/
		//构建分类树形
		$formula_table['price_list'] = self::build_tree($formula_table['price_list'], 0);
		//统计每级分类个数
		$formula_table['price_list'] = self::count_array_childs($formula_table['price_list']);
		$formula_table = self::create_table_data($formula_table);
		$table_data = model_formula::get_formula_table_data($produce['product_id']);
		$table_data = self::create_table_contain($table_data);
		self::smarty() -> assign('table_data', $table_data);
		self::smarty() -> assign('formula_table', $formula_table);
		self::smarty() -> assign('depth_num', self::$_depth_num);
		self::smarty() -> assign('parent_id', $id);
		self::smarty() -> assign('first', 0);
		self::smarty() -> display(self::$dirName . '/formula_table.html');
	}
	//保存表格数据
	public static function table_save(){
		if(!model_admin::islogin()) {
    		self::goto_login();
    	}
		self::operation(array('formula_table'));
		$data = self::get_request_data();
		$parent_id = intval(trim($data['parent_id']));
		if(empty($data['cell_price']) || empty($parent_id)){
			self::ajax_ouput(300, '操作失败,非法操作');
		}
		$dataarr = array();
		$i = 0;
		foreach($data['cell_price'] as $key => $value){
			$dataarr[$i][':formula_id'] = $parent_id;
			$dataarr[$i][':attr_id'] = $key;
			$dataarr[$i][':attr_value'] = json_encode($value);
			$i++;
		}
		$transport = array();
		/*if(!empty($data['transport_price'])){
			$i = 0;
			foreach($data['transport_price'] as $key => $value){
				$transport[$i][':formula_id'] = $parent_id;
				$transport[$i][':attr_id'] = $key;
				$transport[$i][':attr_value'] = json_encode($value);
				$i++;
			}
		}*/
		$result = model_formula::insert_formula_table_data($dataarr, $parent_id, $transport);
		if(!empty($result)){
			self::ajax_ouput(200, '操作成功','formula_table','closeCurrent');
		}else{
			self::ajax_ouput(300, '操作失败');
		} 
	}
	//小类获取
	public static function get_small_class(){
		if(!model_admin::islogin()) {
    		self::goto_login();
    	}
		self::operation(array('formula_table'));
		$id = intval($_GET['id']);
		$return = array();
		if(empty($id)){
			die(json_encode($return));
		}
		$list = model_formula::get_small_class($id);
		if($_GET['method'] == 'all'){
			$return[0][] = "";
			$return[0][] = "所有小类产品";
		}else{
			$return[0][] = "";
			$return[0][] = "请选择小类产品";
		}
		if(!empty($list)){
			foreach($list as $k=>$v){
				$return[$k+1][] = $v['product_id'];
				$return[$k+1][] = $v['produce_name'];
			}
		}
		die(json_encode($return));
	}
	//批量修改数据
	public static function modify_batch(){
		if(!model_admin::islogin()) {
    		self::goto_login();
    	}
		self::operation(array('formula_table'));
		$id = intval($_REQUEST['id']);
		
		if($_POST){
			$data = self::get_request_data();
			unset($data['dwz_rowNum']);
			if(empty($data) || empty($id)){
				self::ajax_ouput(300, '操作失败,请输入修改数据');
			}
			$result = model_formula::modify_batch($id, $data);
			if(!empty($result)){
				self::ajax_ouput(200, '操作成功','formula_table','closeCurrent');
			}else{
				self::ajax_ouput(300, '操作失败');
			} 
		}
		self::smarty() -> assign('id', $id);
		self::smarty() -> display(self::$dirName . '/formula_batch.html');
	}
	//获取材质
	public static function get_merial(){
		if(!model_admin::islogin()) {
    		self::goto_login();
    	}
		self::operation(array('formula_table'));
		$id = intval($_GET['id']);
		$info = model_formula::get_marerial_group($id);
		self::smarty() -> assign('basic_price', $info);
		self::smarty() -> assign('flag', 'basic_price');
		self::smarty() -> display(self::$dirName . '/formula_merial.html');
	}
	//获取地区
	public static function get_area(){
		if(!model_admin::islogin()) {
    		self::goto_login();
    	}
		self::operation(array('formula_table'));
		$id = intval($_GET['id']);
		$info = model_formula::get_area_group($id);
		self::smarty() -> assign('area_price', $info);
		self::smarty() -> assign('flag', 'area_price');
		self::smarty() -> display(self::$dirName . '/formula_merial.html');
	}
	//获取加减选择
	public static function get_action(){
		if(!model_admin::islogin()) {
    		self::goto_login();
    	}
		self::operation(array('formula_table'));
		self::smarty() -> display(self::$dirName . '/formula_merial.html');
	}
	//获取提交数据
	private static function get_formula_data(){
		$formula = self::get_request_data();
		//处理微信端可见用户
		$str = "";
		foreach($formula['visible'] as $value){
			if(empty($formula['visible_'.$value])){
				continue;
			}
			foreach($formula['visible_'.$value] as $v){
				$str .= "{$value}_{$v},";
			}
			unset($formula['visible_'.$value]);
		}
		$formula['visible'] = trim($str, ',');
		if(empty($formula['visible'])){
			self::ajax_ouput(300, '操作失败,请选择微信端可见用户');
		}
		//处理价格字段
		$str = "";
		$price_list = "";
		if(empty($formula['category'])){
			self::ajax_ouput(300, '操作失败,请选择价格字段');
		}
		foreach($formula['category'] as $value){
			if(empty($formula['price_'.$value])){
				continue;
			}
			foreach($formula['price_'.$value] as $v){
				if(preg_match("/^{$value}/",$v)){
					$price_list .= "{$v}/";
					$arr = explode(",", $v);
					$key = $arr[count($arr) - 1];
					$str .= "{$key},";
				}
			}
			unset($formula['price_'.$value],$formula['category']);
		}
		$formula['express'] = trim($str, ',');
		$formula['price_list'] = trim($price_list, '/');
		if(empty($formula['express'])){
			self::ajax_ouput(300, '操作失败,请选择价格字段');
		}
		return $formula;
	}
	//分割地域
	private static function spare_area($str){
		$return = array();
		if(empty($str)){
			return $return;
		}
		$arr = explode(',', $str);
		foreach($arr as $v){
			$return[] = explode('/', $v);
		}
		return $return;
	}

}
